package com.aiju.cash.ui.member.adater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.member.MemberCardActivity;
import com.aiju.cash.ui.member.entity.MemberCard;
import com.aiju.cash.widgit.MemberCardVew;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-02-21 14:27
 * @version: V1.0
 * @Description: 会员卡
 */
public class MemberCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<MemberCard> memberCards;

    private int type;

    public MemberCardAdapter(Context context, List<MemberCard> memberCards, int type) {
        this.type = type;
        this.context = context;
        this.memberCards = memberCards;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_member_card, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holder1 = (ViewHolder) holder;
        MemberCard memberCard = memberCards.get(position);

        holder1.memberCardVew.setCardName(memberCard.getName());
        holder1.memberCardVew.setCardDiscount(memberCard.getDiscount());
        if (memberCard.isForever()) {
            holder1.memberCardVew.showForever();
        } else {
            holder1.memberCardVew.setCardTime(memberCard.getTime());
        }
        holder1.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position);
                }
            }
        });
        if (type == MemberCardActivity.SELECT) {
            holder1.btnView.setText("选择此会员卡");
        }
    }

    @Override
    public int getItemCount() {
        return memberCards.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private View view;
        private MemberCardVew memberCardVew;
        private TextView btnView;

        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            memberCardVew = (MemberCardVew) itemView.findViewById(R.id.card_list);
            btnView = (TextView) itemView.findViewById(R.id.tv_card_info);
        }
    }

    //点击事件
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
